package actions;

import org.apache.commons.lang.StringUtils;

import model.VisitorDTO;
import com.opensymphony.xwork2.ActionSupport;
import dao.DataAccessObject;

public class RegisterAction extends ActionSupport{
	private String firstname;
	private String lastname;
	private String gender;
	private String telephone;
	private String email;
	private String location;
	private String hobby;
	private String description;
	DataAccessObject dao = new DataAccessObject();
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("New visitor's infomation has been added!");
		DataAccessObject dao = new DataAccessObject();
		VisitorDTO vs = new VisitorDTO();
		vs.setFirstname(firstname);
		vs.setLastname(lastname);
		vs.setGender(gender);
		vs.setTelephone(telephone);
		vs.setEmail(email);
		vs.setLocation(location);
		vs.setHobby(hobby);
		vs.setDescription(description);
		dao.insertRegister(vs);
		System.out.println("Added register: " + firstname);
		
		return ActionSupport.SUCCESS;
	}
	
	public void validate(){
		System.out.println("Validating...");
		
		String NAME_PATTERN = "^[^0-9]+$";
		String NUMBER_PATTERN = "^[0-9]+$";
		String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"				
	            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		
		if(firstname == null || firstname.trim().equals("")){
			addFieldError("firstname", "First name is required");
			
		} else if (!firstname.matches(NAME_PATTERN)){
			addFieldError("firstname", "Name must not contain numbers");
			
		} else if (firstname.length() > 20) {
			addFieldError("firstname", "First name is not more than 20 characters");
		}
		
		if(lastname == null || lastname.trim().equals("")){
			addFieldError("lastname", "Last name is required");
			
		} else if (!lastname.matches(NAME_PATTERN)){
			addFieldError("lastname", "Name must not contain numbers");
			
		} else if (lastname.length() > 20) {
			addFieldError("lastname", "Last name is not more than 20 characters");
		}
		
		if(telephone == null){
			addFieldError("telephone", "Telephone is required");
			
		}else if (!telephone.matches(NUMBER_PATTERN)){
			addFieldError("telephone", "Telephone must be a number"); 
			
		}else if (telephone.length() > 11) {
			addFieldError("telephone", "Telephone is not more than 11 characters");
		}
		
		if(email == null || email.trim().equals("")){
			addFieldError("email", "Email is required");
			
		} else if(!email.matches(EMAIL_PATTERN)){
			addFieldError("email", "Please input valid email");
		}
		else if (email.length() > 50) {
			addFieldError("email", "Email is not more than 50 characters");
		}
		
		if(StringUtils.isEmpty(getLocation())){
			addFieldError("location", "Select a location.");
		}
		if(description != null && description.length() > 200) {
			addFieldError("description", "Description is not more than 200 characters.");
		}
	}
	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	private static final long serialVersionUID = 1L;
	
	
}
